#include "../kore2.h"

kore::sdl::SdlSurface::SdlSurface(int width, int height)
{
    Uint32 rmask, gmask, bmask, amask;

    /* SDL interprets each pixel as a 32-bit number, so our masks must depend
       on the endianness (byte order) of the machine */
	#if SDL_BYTEORDER == SDL_BIG_ENDIAN
		rmask = 0xff000000;
		gmask = 0x00ff0000;
		bmask = 0x0000ff00;
		amask = 0x000000ff;
	#else
		rmask = 0x000000ff;
		gmask = 0x0000ff00;
		bmask = 0x00ff0000;
		amask = 0xff000000;
	#endif
	this->m_surface = SDL_CreateRGBSurface( SDL_HWACCEL , width, height, 32, rmask, gmask, bmask, amask );
	this->m_width = width;
	this->m_height = height;
}

kore::sdl::SdlSurface::SdlSurface(SDL_Surface* surface)
{
	this->m_surface = surface;
}

/**
 * @note Possible bug here, as surfaces can be created outside of the class
 * and passed in the constructor. If we delete a surface we didn't create
 * we might run into trouble.
 */
kore::sdl::SdlSurface::~SdlSurface()
{
	SDL_FreeSurface(this->m_surface);
}

SDL_Surface* kore::sdl::SdlSurface::getSurface()
{
	return this->m_surface;
}

void kore::sdl::SdlSurface::setPixel(int x, int y, Uint32 color)
{
	pixelColor(this->m_surface, x, y, color);
}


void kore::sdl::SdlSurface::blit(SDL_Surface* s)
{
	SDL_Rect sr = { 0, 0, 640, 480 };
	SDL_Rect dr = { 0, 0, 640, 480 };
	SDL_BlitSurface(this->m_surface, &sr, s, &dr);
}

void kore::sdl::SdlSurface::blitRegion(SDL_Surface* s, int x, int y, int width, int height)
{
	SDL_Rect sr = { x, y, x+width, y+height };
	SDL_Rect dr = { x, y, x+width, y+height };
	SDL_BlitSurface(this->m_surface, &sr, s, &dr);
}

void kore::sdl::SdlSurface::clear(Uint32 color)
{
	boxColor(this->m_surface,0,0,this->m_width - 1, this->m_height - 1, color);

}
